﻿//监听值的变化，自适应---------------------
$('#inputword').bind('input propertychange', function(a) {
    //console.log(a.target);
    var a= a.target;
    //自适应高度
    console.log(a.rows);
    //if(!row){row=1;};
    var wid=document.getElementById("inputword").style.width;
    //a.cols=parseInt((wid-10)/15);
    if(a.cols==0){a.cols=20}
    var col=a.cols;
    var val=a.value.length;
    var rownum=1;
    console.log("col",col);
    console.log("val.length",val);
    if(val>=col){
        rownum=Math.ceil(val/col);
        console.log("rownum",rownum);
        if(rownum>=3){
            a.style.overflowY="auto";
            a.rows=3;
        }else{
            a.rows=rownum;
        }
    }
});
//表情 输入框不能同时存在-------------------------------
$("#inputword").focus(function(){
    $(".hide").css("display","none");
});
//页面底部定位------------------------------------------
function tobottom(){
    console.log("页面滚动底部方法");
    var chatbody= document.getElementById('chatbody');
    chatbody.scrollTop = chatbody.scrollHeight;
}
//展示大图片--------------------------------------------
function showbigimg(e){
    $("#digimgplace").css("display","block");
    $("#digimgplace").css("background-image","url('"+e.src+"')");
}
//关闭大图----------------------------------------------
$("#digimgplace").click(function(){
    $("#digimgplace").css("display","none");
});
//===================================================获取用户ID=========================================================
var id=$.cookie("MYID");
//var id="7c7fdd2ef9a51aed";
if(id){

}else{
    location.href="index.html";
}
//群聊名称-----------------------------------------------------------------------------------
if($.cookie("allchatname")){
    $(".PageWrapper.nonav .page_title.unified span").html($.cookie("allchatname"));
}
//环信保存用户信息 及 成员信息---------------------------------------------------------------
//
var memberids=[];
var memberimgs={}; //保存群组成员头像

var roomidreg=/(roomid=)([a-zA-Z0-9]*)/;
var roomid = urlfrom.match(roomidreg).splice("=")[2];
console.log("roomid:"+roomid);
var masterid=urlfrom.match(/(master=)([a-zA-Z0-9]*)/).splice("=")[2];//群主信息
//memberids.push(masterid);//插入群主ID

console.log(masterid);
$(".chattitleinput").html($.cookie("allchatname"));
//聊天页面返回键
$("#chatpagegobackbtn").click(function(){
    history.go(-1);
});
//环信建立连接
var conn = new WebIM.connection({
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
});
//获取聊天对象信息
var otherimg="";//保存他人头像
var myimg="";//保存自己头像
//自己的信息
$.ajax({
    type: "post",
    //提交的网址
    url: userurl,
    //提交的数据
    data: { "userId": id},
    //返回数据的格式
    datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
    success:function(data){
        if(data.Data.userAva){
            myimg="<img src='http://42.62.66.198:8003/"+data.Data.userAva+"'>";
        }else{
            myimg="<img src='img/af8.png'>";
        }
    }
});
//时间方法---------------------------------------------------------
function GetDateT(){
    var d,s;
    d = new Date();
    //s = d.getYear() + "-";             //取年份
    //s = s + (d.getMonth() + 1) + "-";//取月份
    //s= d.getDate() + " ";         //取日期
    s = d.getHours() + ":";       //取小时
    s += d.getMinutes() ;    //取分
    //s += d.getSeconds();         //取秒
    return(s);
}
var oldtime=GetDateT();//保存上一次时间
//头像跳转方法-----------------------------------------------------
function gotootherpage(d){
    location.href="otherpeoplepage.html?otherid="+d.dataset.thisid;
    //console.log(d.dataset.thisid);
}
//登录
var options = {
    apiUrl: WebIM.config.apiURL,
    user:id,
    pwd:id,
    appKey: "pulu#pulu"
};
conn.open(options);

//获取群主头像--------------------------------------
//$.ajax({
//    type:"post",
//    dataType: "json",
//    url:gethxmemberurl,
//    data:{"persons":[masterid]},
//    success:function(m){
//        console.log("获取的群主头像");
//        console.log(m);
//        if(m.Data[0].headImg){
//            memberimgs[m.Data[0].userId]=m.Data[0].headImg;
//        }
//    }
//});
//收信息--获取成员头像----------------------------------------------------------------
//获取环信成员信息----------------------------------
var queryRoomMember = function () {
    var member = '';
    conn.queryRoomMember({
        roomId: roomid,
        success: function (members) {
            memberids=[];
            memberids.push(masterid);//插入群主ID
            //成功就清空页面
            $(".memberimgplace .imgbox").remove();
            $("#aa div").remove();
            $("#delaa div").remove();
            if(members&&members.length>0){
                var tim= 0,len=members.length;
                for (var o in members) {
                    tim++;
                    member = members[o].jid;
                    memberids.push(member.match(/[pulu#pulu_0-9a-zA-Z]+/)[0].replace("pulu#pulu_",""));
                    if(tim==len){
                        console.log("保存的群组成员id:",memberids);
                        //获取服务器所有成员信息----------------------------
                        $.ajax({
                            type:"post",
                            dataType: "json",
                            url:gethxmemberurl,
                            data:{"persons":memberids},
                            success:function(m){
                                console.log("获取的成员头像");
                                console.log(m);
                                if(m.Data&&m.Data.length>0){
                                    var tim=0;
                                    for(var i=0;i<m.Data.length;i++){
                                        tim++;
                                        var headimg="img/af8.png";
                                        if(m.Data[i].headImg){
                                            headimg=imgurl+m.Data[i].headImg;
                                        }
                                        $("<div class='imgbox' data-thisid='"+m.Data[i].userId+"' onclick='gotootherpage(this)'> <div> <img src='"+headimg+"'/> </div> <div class='username'>"+m.Data[i].nick+"</div> </div>").insertBefore("#addmemberbtn");
                                        //哈希数组保存头像信息
                                        if(memberimgs[m.Data[i].userId]){

                                        }else{
                                            memberimgs[m.Data[i].userId]=m.Data[i].headImg;
                                        }
                                        //循环结束 获取好友列表信息----------------------------------
                                        if(tim==m.Data.length){
                                            getfriendlist(m);
                                        }
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
    });
};
conn.listen({
    onOpened: function ( message ) {          //连接成功回调
        //如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
        conn.setPresence();
        console.log("连接成功:");
        //获取环信成员信息----------------------------------
        queryRoomMember();
    },
    onTextMessage: function ( message ) {
        console.log("收到的文本消息");
        console.log(message);
        if(message.to==roomid){
            var arr1=message.data;
            arr1=codetoface(arr1);
            //获取接收消息的时间-----------
            var newtime=GetDateT();
            if(newtime==oldtime){
                //获取头像---------------------
                if(memberimgs[message.from]){
                    otherimg="<img src='"+imgurl+memberimgs[message.from]+"' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                    $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                    $(".img").height($(".img").width());
                    tobottom();
                }else{
                    $.ajax({
                        type:"post",
                        dataType: "json",
                        url:gethxmemberurl,
                        data:{"persons":[message.from]},
                        success:function(m){
                            console.log("获取的群主头像");
                            console.log(m);
                            if(m.Data[0].headImg){
                                memberimgs[m.Data[0].userId]=m.Data[0].headImg;
                                otherimg="<img src='"+imgurl+m.Data[0].headImg+"' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                                $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                                $(".img").height($(".img").width());
                                tobottom();
                            }else{
                                otherimg="<img src='img/loseimg.png' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                                $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                                $(".img").height($(".img").width());
                                tobottom();
                            }
                        }
                    });
                }
            }else{
                oldtime=newtime;
                if(memberimgs[message.from]){
                    otherimg="<img src='"+imgurl+memberimgs[message.from]+"' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                    $("#chatbody").append("<div class='otherchatword'><div class='showtime'><span>"+newtime+"</span></div> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                    $(".img").height($(".img").width());
                    tobottom();
                }else{
                    $.ajax({
                        type:"post",
                        dataType: "json",
                        url:gethxmemberurl,
                        data:{"persons":[message.from]},
                        success:function(m){
                            console.log("获取的群主头像");
                            console.log(m);
                            if(m.Data[0].headImg){
                                memberimgs[m.Data[0].userId]=m.Data[0].headImg;
                                otherimg="<img src='"+imgurl+m.Data[0].headImg+"' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                                $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                                $(".img").height($(".img").width());
                                tobottom();
                            }else{
                                otherimg="<img src='img/loseimg.png' data-thisid='"+message.from+"' onclick='gotootherpage(this)'/>";
                                $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                                $(".img").height($(".img").width());
                                tobottom();
                            }
                        }
                    });
                }
            }
        }
    },
    onClosed: function ( message ) {console.log("1");},         //连接关闭回调
    onEmojiMessage: function ( message ) {console.log("2");},   //收到表情消息
    //收到图片消息
    onPictureMessage: function ( message ) {//收到图片消息
        console.log(message);
        var newtime=GetDateT();
        if(newtime==oldtime){
            $("#chatbody").append("<div class='otherchatword'> <div class='img'> "+otherimg+" </div> <div class='word' style='padding:2px;'><p class='arrow'></p><img onclick='showbigimg(this)' class='getimg' src='"+message.url+"' onload='tobottom()'></div> </div>");
            $(".img").height($(".img").width());
        }else{
            oldtime=newtime;
            $("#chatbody").append("<div class='otherchatword'><div class='showtime'><span>"+newtime+"</span></div> <div class='img'> "+otherimg+" </div> <div class='word' style='padding:2px;'><p class='arrow'></p><img onclick='showbigimg(this)' class='getimg' src='"+message.url+"' onload='tobottom()'></div> </div>");
            $(".img").height($(".img").width());
        }

    },
    onCmdMessage: function ( message ) {console.log("4");},     //收到命令消息
    onAudioMessage: function ( message ) {console.log("5");},   //收到音频消息
    onLocationMessage: function ( message ) {console.log("6");},//收到位置消息
    onFileMessage: function ( message ) {console.log("7");},    //收到文件消息
    onVideoMessage: function ( message ) {console.log("8");},   //收到视频消息
    onPresence: function ( message ) {console.log("9");console.log(message);},       //收到联系人订阅请求、处理群组、聊天室被踢解散等消息
    onRoster: function ( message ) {console.log("10");},         //处理好友申请
    onInviteMessage: function ( message ) {console.log("11");},  //处理群组邀请
    onOnline: function () {console.log("本机网络连接成功12");},                  //本机网络连接成功
    onOffline: function () {console.log("本机网络掉线13");}                 //本机网络掉线
    //onError: function ( message ) {console.log("失败回调14");console.log(message)}           //失败回调
});
//发送聊天信息
var sendword=document.getElementById("sendword");//按钮
//点击按钮或者键盘
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        if(document.getElementById("inputword").value&&!document.getElementById("inputword").value.match(/^\s+$/g)){
            var id = conn.getUniqueId();//生成本地消息id
            var msg = new WebIM.message('txt', id);//创建文本消息
            //判断是否有表情
            var arr1=document.getElementById("inputword").value;
            arr1=codetoface(arr1);
            //配置发送到群组
            var option = {
                msg:document.getElementById("inputword").value,             // 消息内容
                to: roomid,                     // 接收消息对象(群组id)
                roomType: false,
                chatType: 'chatRoom',
                success: function () {
                    console.log("发送信息成功");
                    console.log(id);
                    //console.log(serverId);
                    var newtime=GetDateT();
                    console.log(newtime);
                    if(newtime==oldtime){
                        document.getElementById("inputword").rows="1";
                        $("#chatbody").append("<div class='mychatword'><div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                        tobottom();
                        $("#sendword").attr("class","cannotclick");
                        $("#inputword").blur();
                    }else{
                        oldtime=newtime;
                        document.getElementById("inputword").rows="1";
                        $("#chatbody").append("<div class='mychatword'> <div class='showtime'><span>"+newtime+"</span></div><div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                        tobottom();
                        $("#sendword").attr("class","cannotclick");
                        $("#inputword").blur();
                    }
                    console.log($(".img").width());
                    $(".img").height($(".img").width());
                    $("#inputword").val("");
                },
                fail: function () {
                    console.log('failed');
                }
            };
            msg.set(option);
            msg.setGroup('groupchat');
            conn.send(msg.body);
        }
    }
};
sendword.onclick=function(){
    if(document.getElementById("inputword").value&&!document.getElementById("inputword").value.match(/^\s+$/g)){
        var id = conn.getUniqueId();//生成本地消息id
        var msg = new WebIM.message('txt', id);//创建文本消息
        //判断是否有表情
        var arr1=document.getElementById("inputword").value;
        arr1=codetoface(arr1);
        //配置发送到群组
        var option = {
            msg:document.getElementById("inputword").value,             // 消息内容
            to: roomid,                     // 接收消息对象(群组id)
            roomType: false,
            chatType: 'chatRoom',
            success: function () {
                console.log("发送信息成功");
                console.log(id);
                //console.log(serverId);
                var newtime=GetDateT();
                if(newtime==oldtime){
                    document.getElementById("inputword").rows="1";
                    $("#chatbody").append("<div class='mychatword'><div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                    tobottom();
                    $("#sendword").attr("class","cannotclick");
                    $("#inputword").blur();
                }else{
                    oldtime=newtime;
                    document.getElementById("inputword").rows="1";
                    $("#chatbody").append("<div class='mychatword'> <div class='showtime'><span>"+newtime+"</span></div><div class='img'>"+myimg+"</div> <div class='word'><p class='arrow'></p>"+arr1+"</div> </div>");
                    tobottom();
                    $("#sendword").attr("class","cannotclick");
                    $("#inputword").blur();
                }
                console.log($(".img").width());
                $(".img").height($(".img").width());
                $("#inputword").val("");
            },
            fail: function () {
                console.log('failed');
            }
        };
        msg.set(option);
        msg.setGroup('groupchat');
        conn.send(msg.body);
        //=========================================
    }
};
//打开表情
var mySwiper;
$(".face").click(function(){
    if(document.getElementById("facelist").style.display==="block"){
        document.getElementById("facelist").style.display="none";
    }else if(document.getElementById("facelist").style.display==="none"){
        document.getElementById("facelist").style.display="block";
        if(!mySwiper){
            mySwiper = new Swiper ('.swiper-container', {
                paginationClickable: true,
                touchRatio : 1,
                initialSlide :0,
                pagination: '.swiper-pagination'
            })
        }
    }
});
//发送表情
function sendface(e){
    var face=e.dataset.key;
    $("#sendword").attr("class","");
    $("#inputword").insertAtCaret(face);
    $("#inputword").blur();
}
//上传图片
document.getElementById("addimgbtn").addEventListener( 'change',createimg,false );
function createimg(){
    var id = conn.getUniqueId();
    var msg = new WebIM.message('img', id);
    var input = document.getElementById('addimgbtn');//选择图片的input
    var file = WebIM.utils.getFileUrl(input);
    console.log(file);
    var allowType = {
        "jpg": true,
        "jpeg": true,
        "gif": true,
        "png": true,
        "bmp": true
    };
    //alert(roomid);
    if ( file.filetype.toLowerCase() in allowType ) {
        var imgurl="";
        msg.set({
            apiUrl: WebIM.config.apiURL,
            file: file,
            //to: otherid,
            to: roomid,                     // 接收消息对象(群组id)
            roomType: false,
            chatType: 'chatRoom',
            onFileUploadError: function ( error ) {
                console.log("发送图片失败");
            },//图片上传失败
            onFileUploadComplete: function ( data ) {
                console.log("发送图片部分");
                //图片地址：data.uri + '/' + data.entities[0].uuid;
                console.log(data.uri + '/' + data.entities[0].uuid);
                imgurl=data.uri + '/' + data.entities[0].uuid;
            },
            success: function ( id, serverMsgId ) {
                console.log("发送图片成功");
                console.log(id);
                console.log(serverMsgId);
                var newtime=GetDateT();
                if(newtime==oldtime){
                    $("#chatbody").append("<div class='mychatword'> <div class='img'>"+myimg+"</div> <div class='word' style='padding:2px;'><p class='arrow'></p><img class='getimg' onclick='showbigimg(this)' src='"+imgurl+"' onload='tobottom()'></div> </div>");
                }else{
                    oldtime=newtime;
                    $("#chatbody").append("<div class='mychatword'><div class='showtime'><span>"+newtime+"</span></div> <div class='img'>"+myimg+"</div> <div class='word' style='padding:2px;'><p class='arrow'></p><img class='getimg' onclick='showbigimg(this)' src='"+imgurl+"' onload='tobottom()'></div> </div>");
                }

            },//图片消息发送成功
            flashUpload: WebIM.flashUpload
        });
        conn.send(msg.body);
    }
};
//加载好友列表----------------------------------------------
var friendarr=[];
var friendimgarr={};
var friendnamearr={};
function getfriendlist(m){
    friendarr=[];//保存所有好友数据
    $("#maina div").remove();
    $("#aa div").remove();
    $("#delmaina div").remove();
    $("#delaa div").remove();
    $.ajax({
        datatype:"json",
        type:"get",
        url:"http://42.62.66.198:9001/api/PuluGoodFriend/GetByUserId?userId="+id,
        success:function(d){
            //console.log("好友列表");
            //console.log(d);
            if(d.Data.length>0){
                //排序
                friendarr= d.Data;
                function compare(property){
                    return function(a,b){
                        var value1 = pinyin.getCamelChars(a[property]);
                        var value2 = pinyin.getCamelChars(b[property]);
                        return value1.localeCompare(value2);
                    }
                }
                friendarr.sort(compare("friendnick"));
                hasharr={};//判断字母是否存在
                //添加好友列表-------------------------
                for(var i=0;i<friendarr.length;i++){
                    if(friendimgarr[friendarr[i].friendId]){}else{

                        if(friendarr[i].headimg){
                            friendimgarr[friendarr[i].friendId]=imgurl+friendarr[i].headimg;
                        }else{
                            friendimgarr[friendarr[i].friendId]="img/af8.png";
                        }
                        friendnamearr[friendarr[i].friendId]=friendarr[i].friendnick;
                    }
                    friendarr[this.index]=i;
                    //如果昵称开头为汉字或英文字母
                    var key=pinyin.getCamelChars(friendarr[i].friendnick[0]);
                    if(key.match(/[a-z|A-Z]/)){
                        key=key.toLowerCase();
                        //console.log(key);
                        if(!hasharr[pinyin.getCamelChars(friendarr[i].friendnick[0])]){
                            hasharr[pinyin.getCamelChars(friendarr[i].friendnick[0])]=i;
                        }
                        //如果成员已经存在
                        var disword="";
                        //console.log("memberimgs",memberimgs);
                        if(memberimgs[friendarr[i].friendId]){
                            disword="disabled='disabled' checked='checked'"
                        }
                        //console.log("disword",disword);
                        $("#maina").append("<div class='section_header section_header_gray "+key+"' data-num='"+i+"' style='display:none'> <span class='"+key+"'>"+key+"</span> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'"+disword+"><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                        $("#aa").append("<div class='section_header section_header_gray "+key+"' data-num='"+i+"' style='display:none'> <span class='"+key+"'>"+key+"</span> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' "+disword+" data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                    }else{
                        $("#mainjinghao").css("display","block");
                        $("#mainjinghao").append("<div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                        $("#jinghao").css("display","block");
                        $("#jinghao").append("<div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
                    };
                }
                for(var key in hasharr){
                    key=key.toLowerCase();
                    $("#imgchoicefriend .section_header.section_header_gray."+key).eq(0).css("display","block");
                };
                //删除好友列表-------------------------
                var hasharr1={};//判断字母是否存在
                for(var i1=0;i1< m.Data.length;i1++){
                    friendarr[this.index]=i1;
                    //如果昵称开头为汉字或英文字母
                    var key1=pinyin.getCamelChars(m.Data[i1].nick[0]);
                    if(key1.match(/[a-z|A-Z]/)){
                        key1=key1.toLowerCase();
                        //console.log(key1);
                        if(!hasharr1[pinyin.getCamelChars(m.Data[i1].nick[0])]){
                            hasharr1[pinyin.getCamelChars(m.Data[i1].nick[0])]=i1;
                        }
                        $("#delmaina").append("<div class='section_header section_header_gray "+key1+"' data-num='"+i1+"' style='display:none'> <span class='"+key1+"'>"+key1+"</span> </div><div class='friends' id='"+m.Data[i1].userId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+m.Data[i1].userId+"'><img src='http://42.62.66.198:8003/"+m.Data[i1].headImg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+m.Data[i1].nick+"</div> </div>");
                        $("#delaa").append("<div class='section_header section_header_gray "+key1+"' data-num='"+i1+"' style='display:none'> <span class='"+key1+"'>"+key1+"</span> </div><div class='friends' id='"+m.Data[i1].userId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+m.Data[i1].userId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+m.Data[i1].userId+"'><img src='http://42.62.66.198:8003/"+m.Data[i1].headImg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+m.Data[i1].nick+"</div> </div>");
                    }else{
                        $("#delmainjinghao").css("display","block");
                        $("#delmainjinghao").append("<div class='friends' id='"+m.Data[i1].userId+"'> <input type='checkbox' class='addchatpeoplebtn'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+m.Data[i1].userId+"'><img src='http://42.62.66.198:8003/"+m.Data[i1].headImg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+m.Data[i1].nick+"</div> </div>");
                        $("#deljinghao").css("display","block");
                        $("#deljinghao").append("<div class='friends' id='"+m.Data[i1].userId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+m.Data[i1].userId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+m.Data[i1].userId+"'><img src='http://42.62.66.198:8003/"+m.Data[i1].headImg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+m.Data[i1].nick+"</div> </div>");
                    };
                }
                for(var key1 in hasharr1){
                    key1=key1.toLowerCase();
                    $("#delfriend .section_header.section_header_gray."+key1).eq(0).css("display","block");
                };
            }
        }
    });
}

//右侧导航条点击跳转
//字母键
var englishbtn1=document.querySelectorAll(".rightnav1 div.zmbtn");//导航条跳转
var tonum1=0;
for(var en1=0;en1<englishbtn1.length;en1++){
    englishbtn1[en1].onclick=function(e){
        console.log("点击的是："+e.target.innerHTML);
        if($(".section_header.section_header_gray."+e.target.innerHTML).length>0){
            var engtitlenum=parseFloat(hasharr[e.target.innerHTML]);
            var divnum=parseFloat(document.querySelector(".section_header.section_header_gray."+e.target.innerHTML).dataset.num);
            var divheight=$(".friends").eq(0).height();
            var titleheight=$("div.section_header.section_header_gray").eq(0).height();
            heightdiv=titleheight*engtitlenum+divnum*divheight;
            $(window).scrollTop(heightdiv);
            console.log("页面跳转操作")
        }
    };
}
//搜索好友------------------------------------------------------
$("#csearchbtn1").click(function(){//搜索跳转
    var word=$("#csearchinput1").val();
    $("#aa").css("display","none");
    $("#a1 div").remove();
    if(friendarr&&friendarr.length>0&&$("#csearchinput1").val()){
        var word=$("#csearchinput1").val();
        for(var i=0;i<friendarr.length;i++){
            var friendnick="";
            friendnick=friendarr[i].friendnick;
            //如果好友列表中有搜索内容
            if(friendnick.match(word)){
                $("#a1").append("<div class='section_header section_header_gray' data-num='"+i+"' style='display:none'> </div><div class='friends' id='"+friendarr[i].friendId+"'> <input type='checkbox' class='addchatpeoplebtn' data-fid='"+friendarr[i].friendId+"'><div class='imageplace'><a href='otherpeoplepage.html?otherid="+friendarr[i].friendId+"'><img src='http://42.62.66.198:8003/"+friendarr[i].headimg+"' class='topic_photo_img'/> </a> </div> <div class='innerplace tc_text'>"+friendarr[i].friendnick+"</div> </div>");
            }
        }
        $("#a1").css("display","block");
    }
});
//监听好友搜索输入框---------------------------------------------
$("#csearchinput1").bind('input propertychange',function(){
    if(!$("#csearchinput1").val()){
        $("#aa").css("display","block");
        $("#a1").css("display","none");
    }
});
//展示群组成员-------------------------
$("#showmember").click(function(){
    //头部隐藏
    $("#mainpagetitle").css("display","none");
    //聊天部分隐藏
    $("#chatbody").css("display","none");
    //底部隐藏
    $(".bottomnav").css("display","none");
    //展示详情页
    $("#allchatmember").css("display","block");
});
//群组成员详情页返回按钮--------------
$("#allchatmembergoback").click(function(){
    //头部展示
    $("#mainpagetitle").css("display","block");
    //聊天部分展示
    $("#chatbody").css("display","block");
    //底部展示
    $(".bottomnav").css("display","block");
    //隐藏详情页
    $("#allchatmember").css("display","none");
});
//点击添加成员按钮-----------------------
$("#addmemberbtn").click(function(){
    $(memberids).each(function(){
        $("[data-key="+$(this)+"]").attr("disabled",true);
        $("[data-key="+$(this)+"]").attr("checked",true);
    });
    $("#allchatmember").css("display","none");
    $("#imgchoicefriend").css("display","block");
});
//添加好友返回键------------------------
$("#friendgobackbtn").click(function(){
    $("#allchatmember").css("display","block");
    $("#imgchoicefriend").css("display","none");
});
//添加好友列表确定-------------------------- (ok)
$("#imgchoicedFDokbtn1").click(function(){
    var addmemberhash={};//哈希判断存在与唯一性
    var addlist=[];//添加成员数组
    if($("#aa").css("display")=="block"){
        var allcheck=document.querySelectorAll("#aa .addchatpeoplebtn,#a1 .addchatpeoplebtn");
        var tim=0;
        for(var i= 0,len=allcheck.length;i<len;i++){
            tim++;
            if(allcheck[i].checked==true){
                if(allcheck[i].disabled&&allcheck[i].disabled=="disabled"){
                    //不能点的
                }else{
                    if(addmemberhash[allcheck[i].dataset.fid]){
                        //是否存在 搜索列表
                    }else{
                        addmemberhash[allcheck[i].dataset.fid]=allcheck[i].dataset.fid;
                        addlist.push(allcheck[i].dataset.fid);
                    }
                }
            }
            if(tim==len){
                console.log("addlist",addlist);
                //循环结束 添加成员
                var option = {
                    list:addlist,
                    roomId:roomid,
                    success:function(d){
                        console.log("添加成功了",d);
                        //memberids=[];
                        //queryRoomMember();
                        addmemberhash[masterid]=masterid;
                        $(".memberimgplace .imgbox").remove();
                        for(var key in addmemberhash){
                            console.log("添加好友操作");
                            $("<div class='imgbox' data-thisid='"+addmemberhash[key]+"' onclick='gotootherpage(this)'> <div> <img src='"+friendimgarr[addmemberhash[key]]+"'/> </div> <div class='username'>"+friendnamearr[addmemberhash[key]]+"</div> </div>").insertBefore("#addmemberbtn");
                        }
                    }
                };
                conn.addGroupMembers(option);
                //刷新成员页面
                //$(".memberimgplace .imgbox").remove();
                var tim2=0;
                for(var r= 0;r<len;r++){
                    allcheck[r].checked=false;
                    tim2++;
                    //清空操作记录 展开页面
                    if(tim2==len){
                        $("#allchatmember").css("display","block");
                        $("#imgchoicefriend").css("display","none");
                    }
                }
            }
        }
    }
});
//点击删除成员展开页面----------------------------
$("#delmemberbtn").click(function(){
    $("#allchatmember").css("display","none");
    $("#delfriend").css("display","block");
});
//删除成员返回按钮-------------------------
$("#delfriendgobackbtn").click(function(){
    $("#allchatmember").css("display","block");
    $("#delfriend").css("display","none");
});
//删除成员列表确定-------------------------
$("#delimgchoicedFDokbtn1").click(function(){
    var addmemberhash={};//哈希判断存在与唯一性
    var addlist=[];//删除成员数组
    if($("#delaa").css("display")=="block"){
        var allcheck=document.querySelectorAll("#delaa .addchatpeoplebtn,#dela1 .addchatpeoplebtn");
        var tim=0;
        for(var i= 0,len=allcheck.length;i<len;i++){
            tim++;
            if(allcheck[i].checked==true){
                if(addmemberhash[allcheck[i].dataset.fid]){
                    //是否存在 搜索列表
                }else{
                    addmemberhash[allcheck[i].dataset.fid]=allcheck[i].dataset.fid;
                    addlist.push(allcheck[i].dataset.fid);
                }
            }
            if(tim==len){
                //循环结束 删除成员 (只能一个一个删除 用循环)
                var deltim=0;
                for(var d=0;d<addlist.length;d++){
                    deltim++;
                    var option = {
                        affiliation: 'owner',      // 写死
                        roomId:roomid,              // 群组ID
                        success: function () {
                            console.log('删除群聊成员');
                        },
                        to:addlist[d]               // 需要删除的成员ID
                    };
                    conn.addToGroupBlackList(option);
                    //如果循环删除结束了
                    if(deltim==addlist.length){
                        //刷新成员页面
                        //$(".memberimgplace .imgbox").remove();
                        memberids=[];
                        queryRoomMember();
                        var tim2=0;
                        for(var r= 0;r<len;r++){
                            allcheck[r].checked=false;
                            tim2++;
                            //清空操作记录 展开页面
                            if(tim2==len){
                                $("#allchatmember").css("display","block");
                                $("#delfriend").css("display","none");
                            }
                        }
                    }
                }
            }
        }
    }
});
//退出或者解散群---------------------------
console.log(id);
console.log(masterid);
if(id==masterid){  //是群主 就是删除
    $("#delmemberbtn").css("display","block");
    $("#memdelbtn").click(function(){
        var option = {
            reason: '群主删除',
            roomId: roomid,
            success: function () {
                console.log('群主删除群聊');
                location.href="allchat.html";
            }
        };
        conn.destroyGroup(option);
    });
}else{
    $("#memdelbtn").click(function(){
        var option = {
            to: id,
            roomId: roomid,
            success: function () {
                console.log('自己退群');
                location.href="allchat.html";
            },
            error: function () {
                console.log('Leave room faild');
            }
        };
        conn.leaveGroupBySelf(option);
    });
}
//群主改群名-------------------------------
if(id==masterid){
    //展开改名页面
    $("#changenamebtn").click(function(){
        $("#allchatmember").css("display","none");
        $("#changename").css("display","block");
    });
    //改名页面返回
    $("#changenamegoback").click(function(){
        $("#allchatmember").css("display","block");
        $("#changename").css("display","none");
    });
    //有输入值按钮显示
    $('#changetitleinput').bind('input propertychange', function() {
        //判断输入值
        if(document.getElementById("changetitleinput").value&&!document.getElementById("changetitleinput").value.match(/^\s+$/g&&document.getElementById("changetitleinput").value.length>0)){
            $("#changenameokbtn").attr("class","header_action_buttons canclick");
            //改名页面确定
            $("#changenameokbtn").click(function(){
                //修改群组信息
                var option = {
                    roomId:roomid,
                    subject:document.getElementById("changetitleinput").value,    // 群组名称
                    description: '',  // 群组简介
                    success: function () {
                        $(".chattitleinput").html(document.getElementById("changetitleinput").value);
                        $("#changename").css("display","none");
                        $("#allchatmember").css("display","block");
                    }
                };
                conn.changeGroupSubject(option);
            });
        }
        else{
            $("#changenameokbtn").attr("class","header_action_buttons cannotclick");
        }
    });
}